package com.feather.tenia.adapter.saver;

import java.io.CharArrayReader;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Types;

import com.feather.common.data.Column;
import com.feather.common.db.DbSaver;

/**
 * @author flogyin
 */
public class NormalSaver implements DbSaver {
	public static final DbSaver	INSTANCE	= new NormalSaver();

	@Override
	@SuppressWarnings("rawtypes")
	public Class getJavaType() {
		return String.class;
	}

	@Override
	public String getDbType() {
		return "char";
	}

	@Override
	public int getSqlType() {
		return Types.CHAR;
	}

	@Override
	public boolean matchNull() {
		return false;
	}

	@Override
	public boolean save(Object value, PreparedStatement statement, Column column) throws SQLException {
		String str = null;
		if (value != null) {
			str = value.toString();

			char[] chars = str.toCharArray();
			CharArrayReader reader = new CharArrayReader(chars);
			statement.setCharacterStream(column.getOrdinal(), reader, chars.length);
		} else {
			statement.setString(column.getOrdinal(), str);
		}
		return true;
	}
}
